//
//  YYAtmosphereView.m
//  CollarMicrophone
//
//  Created by Yown on 2024/4/9.
//

#import "YYAtmosphereView.h"
#import "YYAtmosphereCell.h"
#import "YYAtmosphereModel.h"

#import <BIToolModule/BIWaterfallLayout.h>
#import <BIToolModule/BIProjectDefine.h>
#import <BIProjectModule/BIAppDefine.h>
#import <BIToolModule/NSArray+Helper.h>

@interface YYAtmosphereView () <UICollectionViewDelegate, UICollectionViewDataSource, BIWaterfallLayoutDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic) NSArray <YYAtmosphereModel *>*dataArray;

@end

@implementation YYAtmosphereView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupViews];
}

- (void)setupViews {
    self.backgroundColor = [UIColor loadColorFromString:@"#F4F4F4"];
    
    BIWaterfallLayout *layout = [BIWaterfallLayout new];
    layout.delegate = self;
    layout.columnCount = 3;
    layout.rowSpacing = 11.0;
    layout.columnSpacing = 11.0;
    layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16);
    
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"YYAtmosphereCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:NSStringFromClass([YYAtmosphereCell class])];
    [self.collectionView reloadData];
}

- (void)reloadModelArray:(NSArray *)modelArray {
    self.dataArray = modelArray;
    [self.collectionView reloadData];
}

#pragma mark - LYWaterfallLayoutDelegate

- (CGFloat)waterfallLayout:(BIWaterfallLayout *)waterfallLayout itemWidth:(CGFloat)itemWidth indexPath:(NSIndexPath *)indexPath {
    return 94.0;
}

#pragma mark - UICollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YYAtmosphereCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YYAtmosphereCell class]) forIndexPath:indexPath];
    YYAtmosphereModel *model = [self.dataArray safeObjectAtIndex:indexPath.row];
    [cell setupModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.dataArray enumerateObjectsUsingBlock:^(YYAtmosphereModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == indexPath.row) {
            obj.isPlay = YES;
            BISafeBlock(self.clickCellBlock, obj);
        } else {
            obj.isPlay = NO;
        }
    }];
    [collectionView reloadData];
}

@end
